﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Royaumes_Libres.Database;

namespace Royaumes_Libres
{
    public partial class register2 : System.Web.UI.Page
    {
        protected Array pLangs;
        protected Array pCountries;
        protected Array pStates;
        protected Array pCities;
        protected string pLang;
        protected string pCountry;
        protected string pState;
        protected string pCity;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillForm();
            }
            else
            {
                if (txtPassword.Text.Trim().Length > 0)
                {
                    this.txtPassword.Attributes["value"] = this.txtPassword.Text;
                    this.txtPasswordAgain.Attributes["value"] = this.txtPasswordAgain.Text;
                }
            }
        }

        protected void FillForm()
        {
            this.pLangs = cCboPop.GetArrayLangs();
            this.pCountries = cCboPop.GetArrayCountries();

            foreach (KeyValuePair<string, string> pLang in pLangs)
            {
                this.cboLang.Items.Add(new ListItem(pLang.Value, pLang.Key));
            }
            foreach (KeyValuePair<string, string> pCountry in pCountries)
            {
                this.cboCountry.Items.Add(new ListItem(pCountry.Value, pCountry.Key));
            }
            this.cboSex.Items.Add(new ListItem("M", "1"));
            this.cboSex.Items.Add(new ListItem("F", "2"));

            this.lblUsername.Text = cText.GetText("Username") + " : ";
            this.lblPassword.Text = cText.GetText("Password") + " : ";
            this.lblPasswordAgain.Text = cText.GetText("PasswordAgain") + " : ";
            this.lblEmail.Text = cText.GetText("Email") + " : ";
            this.lblFirstName.Text = cText.GetText("FirstName") + " : ";
            this.lblLastName.Text = cText.GetText("LastName") + " : ";
            this.lblBirth.Text = cText.GetText("Birthday") + " : ";
            this.lblSex.Text = cText.GetText("Sex") + " : ";
            this.lblCountry.Text = cText.GetText("Country") + " : ";
            this.lblState.Text = cText.GetText("State") + " : ";
            this.cboState.Items.Add(cText.GetText("State"));
            this.cboState.Enabled = false;
            this.lblCity.Text = cText.GetText("City") + " : ";
            this.cboCity.Items.Add(cText.GetText("City"));
            this.cboCity.Enabled = false;
            this.lblLang.Text = cText.GetText("Language") + " : ";
            this.btnRegister.Text = cText.GetText("Register");
        }

        protected void cboCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboState.Items.Clear();
            this.pStates = cCboPop.GetArrayStates(Convert.ToInt32(this.cboCountry.SelectedValue));

            foreach (KeyValuePair<string, string> pState in pStates)
            {
                this.cboState.Items.Add(new ListItem(pState.Value, pState.Key));
            }
            this.cboState.Enabled = true;
        }

        protected void cboState_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.cboCity.Items.Clear();
            this.pCities = cCboPop.GetArrayCities(Convert.ToInt32(this.cboState.SelectedValue));

            foreach (KeyValuePair<string, string> pCity in pCities)
            {
                this.cboCity.Items.Add(new ListItem(pCity.Value, pCity.Key));
            }
            this.cboCity.Enabled = true;
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            bool ValidEmail = cValidate.CheckEmailExist(this.txtEmail.Text);
            bool ValidUsername = cValidate.CheckUsernameExist(this.txtUsername.Text);
            bool ValidPassword = cValidate.CheckPassword(this.txtPassword.Text, this.txtPasswordAgain.Text);
            DateTime Birth;
            string Username = this.txtUsername.Text;
            string Password = this.txtPassword.Text;
            string Email = this.txtEmail.Text;
            string FirstName = this.txtFirstName.Text;
            string LastName = this.txtLastName.Text;
            Birth = new DateTime(Convert.ToInt32(txtBirthY.Text),Convert.ToInt32(txtBirthM.Text),Convert.ToInt32(txtBirthD.Text));
            int Country = Convert.ToInt32(this.cboCountry.SelectedValue);
            int State = Convert.ToInt32(this.cboState.SelectedValue);
            int City = Convert.ToInt32(this.cboCity.SelectedValue);
            int Sex = Convert.ToInt32(this.cboSex.SelectedValue);
            string Lang = this.cboLang.SelectedValue;

            if (ValidEmail && ValidUsername && ValidPassword)
            {
                cUsers.NewUser(Username, Password, Email, FirstName, LastName, Birth, Country, State, City, Sex, Lang);
                cUsers User = cUsers.GetUser(Username, Password);
                this.Session.Add(Properties.Settings.Default.ObjUser, User);
                Response.Redirect("kingdoms.aspx");
            }
        }
    }
}